package com.springboot.infrastructure.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.infrastructure.service.TestService;
import com.springboot.infrastructure.util.MessageConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private MessageConfig messageConfig;

    @Autowired
    private TestService testService;

    @Value("普通字符串")
    private String str;

    //    @Value("${testValue}")
//    private String testvalue;
    private Logger logger = LoggerFactory.getLogger(TestController.class);

    @RequestMapping("/index")
    public String index() {
        return "test";
    }

    @RequestMapping(value = "/testString", produces = "text/plain;charset=UTF-8")
    public String testString() {
        return "中文";
    }

    @RequestMapping("/testValue")
    public JSONObject testValue() {
        JSONObject object = new JSONObject();
        object.put("res", str);
        return object;
    }

    @RequestMapping("/message")
    public String message() {
        String name = messageConfig.getName();
        return name;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestParam Map<String, Object> mapEntity) {
        boolean flag = testService.add(mapEntity);
        return String.valueOf(flag);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@RequestParam Map<String, Object> mapEntity) {
        boolean flag = testService.update(mapEntity);
        return String.valueOf(flag);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public Map<String, Object> detail(@RequestParam("id") Integer id) {
        Map<String, Object> mapEntit = testService.detail(id);
        return mapEntit;
    }

    @RequestMapping(value = "/listPage", method = RequestMethod.POST)
    public Map<String, Object> listPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,
                                        @RequestParam Map<String, Object> parameterMap) {
        Map<String, Object> listPage = testService.listPage(pageNum, pageSize, parameterMap);
        return listPage;
    }

}
